package com.property.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.property.mapper.CarMapper;
import com.property.mapper.ResidentMapper;
import com.property.pojo.Car;
import com.property.pojo.Resident;
import com.property.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarServiceImpl implements CarService {
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private ResidentMapper residentMapper;
    @Override
    public String findAllCar(Integer page) {
        PageHelper.startPage(page,10);
        List<Car> list = carMapper.findAllCar();
        PageInfo pageInfo = new PageInfo(list);
        return JSON.toJSONString(pageInfo);
    }
    //新增车位信息
    @Override
    public String saveCar(Car car,String rName) {
        String message="操作成功";
        int num=0;
        if(car.getcCoder()==null || car.getcCoder().equals("")|| car.getcStatus() == null || car.getcStatus().equals("无状态")){
            message = "不能为空";
        }else {
            if(car.getcStatus()!= null && !car.getcStatus().equals("空闲")){
                Resident resident = residentMapper.findResidentByrName(rName);
                if(resident==null){
                    message = "住户不存在";
                    return JSON.toJSONString(message);
                }else{
                    car.setcRid(resident.getrId());
                }
            }
            if(car.getcId() != null && car.getcId() != 0 ){
                num = carMapper.updateCar(car);
            }else{
                num = carMapper.saveCar(car);
            }
        }
        return JSON.toJSONString(message);
    }

    //查询car 通过id
    @Override
    public String findCarBycId(Integer cId) {
        return JSON.toJSONString(carMapper.findCarBycId(cId));
    }

    //删除车位信息
    @Override
    public String deleteCar(Integer cId) {
        int num = carMapper.deleteCar(cId);
        return JSON.toJSONString(num>0?"操作成功":"操作失败");
    }
}
